package cn.peach.org.service.impl;

import cn.peach.basic.service.impl.BaseServiceImpl;
import cn.peach.basic.util.PageList;
import cn.peach.org.domain.Department;
import cn.peach.org.mapper.DepartmentMapper;
import cn.peach.org.query.DepartmentQuery;
import cn.peach.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;



    @Override
    @Transactional
    public void insert(Department department) {
        departmentMapper.add(department);
        update(department);
    }

    @Override
    @Transactional
    public void update(Department department) {
        if (department.getParent() == null || department.getParent().getId() == null){
            department.setDirPath("/" + department.getId());
        }else {
            Department loadById = departmentMapper.loadById(department.getParent().getId());
            String dirPath = loadById.getDirPath();
            department.setDirPath(dirPath + "/" + department.getId());
        }
        departmentMapper.update(department);
    }

    @Override
    public List<Department> queryTree() {
        return departmentMapper.loadTree();
    }
}
